﻿using Bridge.Shared.Enums.IdentityService;
using Bridge.Shared.Models;
using Bridge.System.Domain;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using SysConfigs;
using SysConfigs.Output;
using SysFiles;
using SysFiles.Input;
using SysFiles.Output;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Bridge.System.Application
{
    public class SysFileService : ApplicationService, ISysFileService
    {
        private IRepository<SysFile, Guid> _sysFileRepository;
        private SysOptions _sysOptions;
        public SysFileService(IRepository<SysFile, Guid> sysFileRepository,IOptions<SysOptions> sysOptions) {
        
        _sysFileRepository = sysFileRepository;
            _sysOptions = sysOptions.Value;
        }
        public Task<ResponseResult> DeleteFileByUrl(List<string> urls)
        {
            throw new NotImplementedException();
        }

        public Task<ResponseResult> DownloadFile(string path, long fileId = 0)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取文件列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResponseResult<SysFileOutput>> GetSysFilesPageList(SysFilePageInput input)
        {
            var query = (await _sysFileRepository.GetQueryableAsync())
                        .WhereIf(!string.IsNullOrEmpty(input.FileName), a => a.FileName.Contains(input.FileName))
                        .WhereIf(input.StoreType.HasValue, a => a.StoreType == input.StoreType)
                        .WhereIf(input.UploadStartTime.HasValue, a => a.CreationTime >= input.UploadStartTime.Value)
                        .WhereIf(input.UploadEndTime.HasValue,a=>a.CreationTime<=input.UploadEndTime);

            var totalCount = query.Count(); ;
            var list = await query.Skip(input.PageSize * input.PageIndex).Take(input.PageSize).ToListAsync();
            var dto = list.Adapt<List<SysFileOutput>>();

            return PageResponseResult<SysFileOutput>.Success("ok", dto, input.PageSize, input.PageIndex, totalCount);
        }

        public async Task<ResponseResult> UploadFile(UploadFileInput input)
        {
            //校验文件大小、类型
            if(input == null || input.file == null)
            {
                return ResponseResult.Fail("上传文件不能为空");
            }
            SysFile sysfile = new();
            IFormFile formFile = input.file;
            string fileExt = Path.GetExtension(formFile.FileName);//文件后缀
            double fileSize = Math.Round(formFile.Length / 1024.0, 2);//文件大小KB
            if (_sysOptions.UploadFile.NotAllowedExt.Contains(fileExt))
            {
                return ResponseResult.Fail("上传失败，未经允许上传类型！");
            }
            if (fileSize > _sysOptions.UploadFile.MaxSize) {
                return ResponseResult.Fail("上传失败，文件大小超出限制！");
            }

            //根据配置存储类型上传文件到不同的地方
            StoreTypeEnum storeType = (StoreTypeEnum)_sysOptions.UploadFile.StoreType;
            switch(storeType)
            {
                case StoreTypeEnum.LOCAL:
                    break;
                case StoreTypeEnum.Minio:
                    break;
                case StoreTypeEnum.ALIYUN:
                    break;
            }


                return ResponseResult<ResponseResult>.Success("ok");
        }
    }
}
